package com.maoye.ossp.bpm.defmanager.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.hotent.bpmx.api.def.BpmDefinitionAccessor;
import com.hotent.bpmx.api.model.process.nodedef.BpmNodeDef;
import com.hotent.bpmx.api.service.BpmDefinitionService;
import com.maoye.ossp.bpm.defmanager.service.IOsspBpmNodeDefService;
import com.maoye.ossp.common.util.RequestContext;
import com.maoye.ossp.common.web.BaseController;

/**
 * 流程节点定义 控制器
 * 
 * @author zxpfss
 * 
 */
@Controller
@RequestMapping(value = "/bpm/def/node/")
public class BpmNodeDefController extends BaseController {
	// ----------------------属性-------------------------
	
	private static Logger logger = Logger.getLogger(BpmNodeDefController.class);
	// 控制器路径
	protected final String rootPath;

	@Resource
	private BpmDefinitionService bpmDefinitionService;

	@Resource
	private BpmDefinitionAccessor bpmDefinitionAccessor;
	
	@Resource
	private IOsspBpmNodeDefService osspBpmNodeDefService;

	public BpmNodeDefController() {
		logger.debug("加载流程根路径");
		RequestMapping rootMapping = AnnotationUtils.findAnnotation(this.getClass(), RequestMapping.class);
		if (rootMapping != null) {
			String rp = rootMapping.value()[0];
			rootPath = rp;
		} else {
			rootPath = "";
		}
	}

	// ----------------------public-------------------------

	/**
	 * 展示节点信息
	 * @param request
	 * @param proDefId 流程定义
	 * @param nodeDefId	节点定义
	 * @return
	 */
	@RequestMapping("showNodeDef")
	public ModelAndView showNodeDef(WebRequest request, @RequestParam("proDefId") String proDefId,
			@RequestParam("nodeDefId") String nodeDefId) {
		ModelAndView mv = null;
		try {
			// 获取节点
			BpmNodeDef bpmNodeDef = bpmDefinitionService.getBpmNodeDefByDefIdNodeId(proDefId, nodeDefId);
			Object dataInfo = osspBpmNodeDefService.getPluginInfo(bpmNodeDef);
			if(dataInfo != null){
				request.setAttribute("dataInfo", JSONObject.fromObject(dataInfo), WebRequest.SCOPE_REQUEST);
			}else{
				request.setAttribute("dataInfo", new JSONObject(), WebRequest.SCOPE_REQUEST);
			}
			return new ModelAndView(rootPath + "/showNodeDef", "model", bpmNodeDef);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return mv;
	}
	
	/**
	 * 保存节点定义
	 * @param request
	 * @return
	 */
	@RequestMapping("saveNode")
	public String saveNode(WebRequest request){
		HttpServletRequest httpRequest = ((ServletWebRequest)request).getRequest();
		try {
			osspBpmNodeDefService.saveNodeDef(new RequestContext(httpRequest));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/common/success";
	}
	// ----------------------private-------------------------
	
	// ----------------------get/set-------------------------
}
